﻿#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;

class Maker {
public:
	Maker() {
		cout << "无参构造函数" << endl;
		a = 10;
	}
	Maker(const Maker &m) {
		cout << "拷贝构造函数" << endl;
		this->a = m.a;
	}
	void print() {
		cout << a << endl;
	}
private:
	int a;
};

void test01() {
	Maker m1;
	m1.print();
	Maker m2(m1);
	m2.print();
}

class Maker2 {
public:
	Maker2() {
		cout << "无参构造函数" << endl;
		a = 10;
	}
	// 编译器提供了默认的拷贝构造函数，如下，进行了成员变量的简单赋值操作 
	/*Maker2(const Maker2 &m) {
		this->a = m.a;
	}*/
	void print() {
		cout << a << endl;
	}
private:
	int a;
};

void test02() {
	Maker2 m1;
	m1.print();
	Maker2 m2(m1);
	m2.print();
}

class Maker3 {
public:
	Maker3() {
		cout << "无参构造" << endl;
	}
	Maker3(int a) {
		cout << "有参构造" << endl;
		this->a = a;
	}
	// 拷贝构造函数必须传引用类型 
	/*Maker3(const Maker3 m3) { // 传递类类型会进入死循环 
		a = m3.a;
	}*/
	Maker3(const Maker3 &m) {
		cout << "拷贝构造" << endl;
		a = m.a;
	}
private:
	int a; 
};

void test03() {
	Maker3 m1(5); // 调用有参构造 
	Maker3 m2(m1); // 调用拷贝构造 
	Maker3 m3 = m1; // 调用拷贝构造 
	Maker3 m4 = 6;  // 调用有参构造  
	Maker3 m5; // 无参构造 
	m5 = m4;  // 赋值操作  
}

int main()
{

	// test01(); 
	// test02();
	test03(); 

	system("pause");
	return EXIT_SUCCESS;
}